home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / kgame / kgamepropertyarray.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-10-10  |  7.4 KB  |  310 lines

  1. /*
  2.     This file is part of the KDE games library
  3.     Copyright (C) 2001 Martin Heni (martin@heni-online.de)
  4.     Copyright (C) 2001 Andreas Beckermann (b_mann@gmx.de)
  5.  
  6.     This library is free software; you can redistribute it and/or
  7.     modify it under the terms of the GNU Library General Public
  8.     License version 2 as published by the Free Software Foundation.
  9.  
  10.     This library is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.     Library General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU Library General Public License
  16.     along with this library; see the file COPYING.LIB.  If not, write to
  17.     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  18.     Boston, MA 02110-1301, USA.
  19. */
  20.  
  21. #ifndef __KGAMEPROPERTYARRAY_H_
  22. #define __KGAMEPROPERTYARRAY_H_
  23.  
  24. #include <qdatastream.h>
  25. #include <kdebug.h>
  26.  
  27. #include "kgamemessage.h"
  28. #include "kgameproperty.h"
  29. #include "kgamepropertyhandler.h"
  30.  
  31.  
  32. template<class type>
  33. class KGamePropertyArray : public QMemArray<type>, public KGamePropertyBase
  34. {
  35. public:
  36.   KGamePropertyArray() :QMemArray<type>(), KGamePropertyBase()
  37.   {
  38.     //kdDebug(11001) << "KGamePropertyArray init" << endl;
  39.   }
  40.   
  41.   KGamePropertyArray( int size )
  42.   {
  43.     resize(size);
  44.   }
  45.   
  46.   KGamePropertyArray( const KGamePropertyArray<type> &a ) : QMemArray<type>(a)
  47.   {
  48.   }
  49.   
  50.   bool  resize( uint size )
  51.   {
  52.     if (size!=QMemArray<type>::size())
  53.     {
  54.       bool a=true;
  55.       QByteArray b;
  56.       QDataStream s(b, IO_WriteOnly);
  57.       KGameMessage::createPropertyCommand(s,KGamePropertyBase::IdCommand,id(),CmdResize);
  58.       s << size ;
  59.       if (policy()==PolicyClean || policy()==PolicyDirty)
  60.       {
  61.         if (mOwner)
  62.         {
  63.           mOwner->sendProperty(s);
  64.         }
  65.       }
  66.       if (policy()==PolicyLocal || policy()==PolicyDirty)
  67.       {
  68.         extractProperty(b);
  69. //        a=QMemArray<type>::resize(size);// FIXME: return value!
  70.       }
  71.       return a;
  72.     }
  73.     else return true;
  74.   }
  75.  
  76.   void setAt(uint i,type data)
  77.   {
  78.     QByteArray b;
  79.     QDataStream s(b, IO_WriteOnly);
  80.     KGameMessage::createPropertyCommand(s,KGamePropertyBase::IdCommand,id(),CmdAt);
  81.     s << i ;
  82.     s << data;
  83.     if (policy()==PolicyClean || policy()==PolicyDirty)
  84.     {
  85.       if (mOwner)
  86.       {
  87.         mOwner->sendProperty(s);
  88.       }
  89.     }
  90.     if (policy()==PolicyLocal || policy()==PolicyDirty)
  91.     {
  92.       extractProperty(b);
  93.     }
  94.     //kdDebug(11001) << "KGamePropertyArray setAt send COMMAND for id="<<id() << " type=" << 1 << " at(" << i<<")="<<data  << endl;
  95.   }
  96.  
  97.   type at( uint i ) const
  98.   {
  99.     return QMemArray<type>::at(i);
  100.   }
  101.   
  102.   type operator[]( int i ) const
  103.   {
  104.     return QMemArray<type>::at(i);
  105.   }
  106.  
  107.   KGamePropertyArray<type> &operator=(const KGamePropertyArray<type> &a)
  108.   {
  109.     return assign(a);
  110.   }
  111.  
  112.   bool  truncate( uint pos )
  113.   {
  114.     return resize(pos);
  115.   }
  116.   
  117.   bool  fill( const type &data, int size = -1 )
  118.   {
  119.     bool r=true;
  120.     QByteArray b;
  121.     QDataStream s(b, IO_WriteOnly);
  122.     KGameMessage::createPropertyCommand(s,KGamePropertyBase::IdCommand,id(),CmdFill);
  123.     s << data;
  124.     s << size ;
  125.     if (policy()==PolicyClean || policy()==PolicyDirty)
  126.     {
  127.       if (mOwner)
  128.       {
  129.         mOwner->sendProperty(s);
  130.       }
  131.     }
  132.     if (policy()==PolicyLocal || policy()==PolicyDirty)
  133.     {
  134.       extractProperty(b);
  135. //      r=QMemArray<type>::fill(data,size);//FIXME: return value!
  136.     }
  137.     return r;
  138.   }
  139.  
  140.   KGamePropertyArray<type>& assign( const KGamePropertyArray<type>& a )
  141.   {
  142. // note: send() has been replaced by sendProperty so it might be broken now!
  143.     if (policy()==PolicyClean || policy()==PolicyDirty)
  144.     {
  145.       sendProperty();
  146.     }
  147.     if (policy()==PolicyLocal || policy()==PolicyDirty)
  148.     {
  149.       QMemArray<type>::assign(a);
  150.     }
  151.     return *this;
  152.   }
  153.   KGamePropertyArray<type>& assign( const type *a, uint n )
  154.   {
  155.     if (policy()==PolicyClean || policy()==PolicyDirty)
  156.     {
  157.       sendProperty();
  158.     }
  159.     if (policy()==PolicyLocal || policy()==PolicyDirty)
  160.     {
  161.       QMemArray<type>::assign(a,n);
  162.     }
  163.     return *this;
  164.   }
  165.   KGamePropertyArray<type>& duplicate( const KGamePropertyArray<type>& a )
  166.   {
  167.     if (policy()==PolicyClean || policy()==PolicyDirty)
  168.     {
  169.       sendProperty();
  170.     }
  171.     if (policy()==PolicyLocal || policy()==PolicyDirty)
  172.     {
  173.       QMemArray<type>::duplicate(a);
  174.     }
  175.     return *this;
  176.   }
  177.   KGamePropertyArray<type>& duplicate( const type *a, uint n )
  178.   {
  179.     if (policy()==PolicyClean || policy()==PolicyDirty)
  180.     {
  181.       sendProperty();
  182.     }
  183.     if (policy()==PolicyLocal || policy()==PolicyDirty)
  184.     {
  185.       QMemArray<type>::duplicate(a,n);
  186.     }
  187.     return *this;
  188.   }
  189.   KGamePropertyArray<type>& setRawData( const type *a, uint n )
  190.   {
  191.     if (policy()==PolicyClean || policy()==PolicyDirty)
  192.     {
  193.       sendProperty();
  194.     }
  195.     if (policy()==PolicyLocal || policy()==PolicyDirty)
  196.     {
  197.       QMemArray<type>::setRawData(a,n);
  198.     }
  199.     return *this;
  200.   }
  201.   void sort()
  202.   {
  203.     QByteArray b;
  204.     QDataStream s(b, IO_WriteOnly);
  205.     KGameMessage::createPropertyCommand(s,KGamePropertyBase::IdCommand,id(),CmdSort);
  206.     if (policy()==PolicyLocal || policy()==PolicyDirty)
  207.     {
  208.       if (mOwner)
  209.       {
  210.         mOwner->sendProperty(s);
  211.       }
  212.     }
  213.     if (policy()==PolicyLocal || policy()==PolicyDirty)
  214.     {
  215.       extractProperty(b);
  216.     }
  217.   }
  218.  
  219.   void load(QDataStream& s)
  220.   {
  221.     //kdDebug(11001) << "KGamePropertyArray load " << id() << endl;
  222.     type data;
  223.     for (unsigned int i=0; i<QMemArray<type>::size(); i++) 
  224.     {
  225.       s >> data;
  226.       QMemArray<type>::at(i)=data;
  227.     }
  228.     if (isEmittingSignal())
  229.     {
  230.       emitSignal();
  231.     }
  232.   }
  233.   void save(QDataStream &s)
  234.   {
  235.     //kdDebug(11001) << "KGamePropertyArray save "<<id() << endl;
  236.     for (unsigned int i=0; i<QMemArray<type>::size(); i++) 
  237.     {
  238.       s << at(i);
  239.     }
  240.   }
  241.  
  242.   void command(QDataStream &s,int cmd,bool)
  243.   {
  244.     KGamePropertyBase::command(s, cmd);
  245.     //kdDebug(11001) << "Array id="<<id()<<" got command ("<<cmd<<") !!!" <<endl; 
  246.     switch(cmd)
  247.     {
  248.       case CmdAt:
  249.       {
  250.         uint i;
  251.         type data;
  252.         s >> i >> data;
  253.         QMemArray<type>::at(i)=data;
  254.         //kdDebug(11001) << "CmdAt:id="<<id()<<" i="<<i<<" data="<<data <<endl; 
  255.         if (isEmittingSignal()) 
  256.         {
  257.           emitSignal();
  258.         }
  259.         break;
  260.       }
  261.       case CmdResize:
  262.       {
  263.         uint size;
  264.         s >> size;
  265.         //kdDebug(11001) << "CmdResize:id="<<id()<<" oldsize="<<QMemArray<type>::size()<<" newsize="<<size <<endl; 
  266.         if (QMemArray<type>::size() != size)
  267.         {
  268.           QMemArray<type>::resize(size);
  269.         }
  270.         break;
  271.       }
  272.       case CmdFill:
  273.       {
  274.         int size;
  275.         type data;
  276.         s >> data >> size;
  277.         //kdDebug(11001) << "CmdFill:id="<<id()<<"size="<<size <<endl; 
  278.         QMemArray<type>::fill(data,size);
  279.         if (isEmittingSignal()) 
  280.         {
  281.           emitSignal();
  282.         }
  283.         break;
  284.       }
  285.       case CmdSort:
  286.       {
  287.         //kdDebug(11001) << "CmdSort:id="<<id()<<endl; 
  288.         QMemArray<type>::sort();
  289.         break;
  290.       }
  291.       default: 
  292.         kdError(11001) << "Error in KPropertyArray::command: Unknown command " << cmd << endl;
  293.         break;
  294.     }
  295.   }
  296. protected:
  297.   void extractProperty(const QByteArray& b)
  298.   {
  299.     QDataStream s(b, IO_ReadOnly);
  300.     int cmd;
  301.     int propId;
  302.     KGameMessage::extractPropertyHeader(s, propId);
  303.     KGameMessage::extractPropertyCommand(s, propId, cmd);
  304.     command(s, cmd, true);
  305.   }
  306.  
  307. };
  308.  
  309. #endif
  310.